import { h } from 'vue'
import { Modal, ModalConfig } from '@arco-design/web-vue'

// 铁行渠道拦截器，保证直连渠道订单同步状态一致
// 各个操作的拦截，给出弹框提示，做了统一封装
export default function useTieXingInterceptor(record: OrderRecord, operateType: string, callback?: (e?: Event) => void) {
  const isTieXing = _isTiexingOrder(record)
  if (!isTieXing) return false

  let isTrue = true

  const modalOpt: ModalConfig = {
    content: '',
    title: '提示',
    okText: '去匹配',
    hideCancel: true,
    onOk: callback,
  }

  switch (operateType) {
    // 已完成、已还车不调用铁行接口
    case 'notCallTiexing':
      modalOpt.content = '无法变更铁行平台已还车和已完成的订单的车辆。请确认是否只在启程修改车牌号？'
      modalOpt.okText = '确认'
      modalOpt.hideCancel = false
      break
    // 还车确认
    case 'returnCarSubmit':
    // 换车确认
    case 'changeCarSubmit':
    // 交车确认
    case 'giveCarSubmit':
    // 订单改排
    case 'editShortOrderReschedule':
    // 修改车牌号
    case 'editShortOrderLicenseNo':
    // 修改订单
    case 'editShortOrder':
      modalOpt.content = () => {
        return h('p', null, [
          h('span', null, '订单'),
          h('span', { style: { color: '#3491fa' } }, record.channelOrderCode),
          h('span', null, '在铁行平台'),
          h('span', { style: { color: '#f76964ff' } }, _getErrorTitle(operateType)),
          h('span', null, '请前往铁行平台查看'),
          h('span', { style: { color: '#3491fa' } }, record.carLicenseNo),
          h('span', null, '的车辆情况。'),
        ])
      }
      modalOpt.okText = '我已知晓'
      break
    // 交车 启程的车辆信息为空的待用车进行拦截
    case 'givecar':
      modalOpt.content = '请先完成当前渠道订单车辆的匹配，再进行交车操作。'
      modalOpt.hideCancel = false
      isTrue = !record.carModelName && record.orderStatus == 100

      break
    // 还车
    case 'returnCar':
      modalOpt.content = '请先完成当前渠道订单车辆的匹配，再进行还车操作。'
      modalOpt.hideCancel = false
      isTrue = !record.carModelName && record.orderStatus == 200
      break
    // 订单换车
    case 'changecar':
      modalOpt.content = '请先完成当前渠道订单车辆的匹配，再进行换车操作。'
      modalOpt.hideCancel = false
      isTrue = !record.carModelName && record.orderStatus == 200
      break
  }

  // 开启弹框
  isTrue &&
    Modal.info({
      ...modalOpt,
      maskClosable: false,
      cancelText: '关闭',
    })
  return isTrue
}

const ERROR_TITLE: ErrorTitleType = {
  giveCarSubmit: '交车失败！',
  returnCarSubmit: '还车失败！',
  changeCarSubmit: '更换车辆失败！',
}
const _getErrorTitle = (operateStr: string) => {
  return ERROR_TITLE[operateStr] || '更换车辆失败！'
}

/**
 * 铁行渠道 -> channelType == -3
 * 渠道订单 -> orderSource == 2
 * 自动维护 -> channelOrderMaintenance == 1
 * 订单状态 -> orderStatus 100 待用车 200 用车中
 */
// 铁行渠道并且是自动维护的订单
const _isTiexingOrder = (orderInfo: any) => {
  return orderInfo.channelOrderMaintenance == 1 && orderInfo.channelType == -3
}

interface OrderRecord {
  carLicenseNo: string
  channelOrderCode: string
  channelType: number
  channelOrderMaintenance: number
  orderStatus: number
  orderSource: number
  carModelName: string
}

type ErrorTitleType = {
  [key: string]: string
}
